package com.gossip;

import java.net.InetSocketAddress;

import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;

public abstract class GossipMember {

	protected String _host;
	
	protected int _port;
	protected Date _heartbeat;
	
	protected int _memberId = 1; 
	protected int _failedCount = 0;
	protected String _name = "";
	protected int _groupId = -1;
	public GossipMember(String host, int port, Date heartbeat, String name) 
	{
		_host = host;
		_port = port;
		_heartbeat = heartbeat;
		this._name = name;
		_memberId = name.hashCode()%1024;
	}
	
	public GossipMember(int memberId, String host, int port, Date heartbeat, String name) 
	{
		_host = host;
		_port = port;
		_heartbeat = heartbeat;
		this._name = name;
		_memberId = name.hashCode()%1024;
	}
	
	public void setFailedCount(int count)
	{
		this._failedCount = count;
	}
	
	public String getHost() {
		return _host;
	}
	

	public int getPort() {
		return _port;
	}

	
	public int getGroupId() {
		return _groupId;
	}
	
	public void setGroupId(int groupId) {
		this._groupId = groupId;
	}
	
	public String getAddress() {
		return _host+":"+_port;
	}

	public Date getHeartbeat() {
		return _heartbeat;
	}
	
	public int getMemberId()
	{
		return _memberId;
	}
	
	public void setHeartbeat(Date heartbeat) {
		this._heartbeat = heartbeat;
	}
	
	public void setHeartbeat() {
		this._heartbeat = new Date();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "Member [memberId = "+_memberId+", address=" + getAddress() + ", heartbeat=" + _heartbeat.toString()+", failedCount="+_failedCount + "]";
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		String address = getAddress();
		result = prime * result
		+ ((address == null) ? 0 : address.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			System.err.println("equals(): obj is null.");
			return false;
		}
		if (! (obj instanceof GossipMember) ) {
			System.err.println("equals(): obj is not of type GossipMember.");
			return false;
		}
		return false;
	}

	public JSONObject toJSONObject() {
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("name", _name);
			jsonObject.put("memberId", _memberId);
			jsonObject.put("host", _host);
			jsonObject.put("port", _port);
			jsonObject.put("heartbeat", _heartbeat.getTime());
			jsonObject.put("failedCount", _failedCount);
			
			return jsonObject;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public JSONObject toJSONObjectWithGroupId() {
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("name", _name);
			jsonObject.put("memberId", _memberId);
			jsonObject.put("host", _host);
			jsonObject.put("port", _port);
			jsonObject.put("heartbeat", _heartbeat.getTime());
			jsonObject.put("failedCount", _failedCount);
			jsonObject.put("groupId", _groupId);
			
			return jsonObject;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
}
